package com.clothes.adp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.clothes.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/5 0005.
 */

public class ItemAdapter extends BaseAdapter {

    private Context context;
    private List<String> list;
    private int clickItem = -1;

    public ItemAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<String> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_one, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }
        String item = list.get(i);
        holder.tvItem.setText(item);
        if(clickItem == i){
            holder.tvItem.setBackgroundResource(R.drawable.conor_blue);
        }else {
            holder.tvItem.setBackgroundResource(R.drawable.conor_gray);
        }

        holder.tvItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(clickItem == i){

                }else {
                    clickItem = i;
                    if(mItemClick != null)
                        mItemClick.click(i);
                    notifyDataSetChanged();
                }
            }
        });
        return view;
    }

    public class ViewHolder {
        @Bind(R.id.tv_item)
        TextView tvItem;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public String getClickItem(){
        if(clickItem == -1){
            return "";
        }
        return list.get(clickItem);
    }


    private onItemClick mItemClick;
    public interface onItemClick{
        void click(int i);
    }

    public void setItemClick(onItemClick itemClick) {
        mItemClick = itemClick;
    }
}
